package view.modifiers;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.events.CSLOSEvent;
import constants.Icons;
import controller.CSController;

public class LOSEventModifier extends JPanel implements Modifier,
	FocusListener, ActionListener, ChangeListener {

	private JTextField min;
	private JTextField sec;
	
	private JSlider slider;
	
	private JButton leftFrame;
	private JButton rightFrame;
	
	private JTextField waitField;
	
	private CSLOSEvent event;
	
	private CSController csc;
	
	public LOSEventModifier(CSLOSEvent event_, double maxTime, CSController cont_) {
		
		event = event_;
		csc = cont_;
		
		JPanel timeRow = new JPanel();
		JPanel slideRow = new JPanel();
		JPanel waitFieldRow = new JPanel();
		JPanel temp = new JPanel();
		
		waitField = new JTextField(""+((int)(event.getEndTime()-event.getStartTime())*100)/100d);
		waitField.setMaximumSize(new Dimension(60,25));
		waitField.setMinimumSize(new Dimension(60,25));
		
		waitField.addFocusListener(this);
		
		double minDbl = (int)(event.getStartTime()/60);
		double secDbl = event.getStartTime()-minDbl*60;
		
		min = new JTextField(""+((int)minDbl), 3);
		sec = new JTextField(""+((int)secDbl), 3);
		
		min.setEditable(false);
		sec.setEditable(false);
		
		slider = new JSlider(SwingConstants.HORIZONTAL,(int)maxTime*100);
		slider.setValue((int)(100*(secDbl+60*minDbl)));
		slider.addChangeListener(this);
		
		leftFrame = new JButton(Icons.getIcon("leftFrame"));
		
		leftFrame.setActionCommand("leftFrame");
		leftFrame.setToolTipText("Decrements one frame.");
		leftFrame.addActionListener(this);
		
		rightFrame = new JButton(Icons.getIcon("rightFrame"));
		
		rightFrame.setActionCommand("rightFrame");
		rightFrame.setToolTipText("Increments one frame.");
		rightFrame.addActionListener(this);
		
		leftFrame.setMaximumSize(new Dimension(leftFrame.getIcon().getIconWidth()+4,
				leftFrame.getIcon().getIconHeight()+4));
		leftFrame.setMinimumSize(new Dimension(leftFrame.getIcon().getIconWidth()+4,
				leftFrame.getIcon().getIconHeight()+4));
		rightFrame.setMaximumSize(new Dimension(rightFrame.getIcon().getIconWidth()+4,
				rightFrame.getIcon().getIconHeight()+4));
		rightFrame.setMinimumSize(new Dimension(rightFrame.getIcon().getIconWidth()+4,
				rightFrame.getIcon().getIconHeight()+4));
		min.setMaximumSize(new Dimension(30,20));
		sec.setMaximumSize(new Dimension(30,20));
		
		slider.setMaximumSize(new Dimension(100,20));
		
		timeRow.setLayout(new BoxLayout(timeRow, BoxLayout.LINE_AXIS));
		slideRow.setLayout(new BoxLayout(slideRow, BoxLayout.LINE_AXIS));
		waitFieldRow.setLayout(new BoxLayout(waitFieldRow, BoxLayout.LINE_AXIS));
		temp.setLayout(new BoxLayout(temp, BoxLayout.PAGE_AXIS));
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		timeRow.add(min);
		timeRow.add(new JLabel(" min "));
		timeRow.add(sec);
		timeRow.add(new JLabel(" sec."));
		
		slideRow.add(leftFrame);
		slideRow.add(Box.createHorizontalStrut(3));
		slideRow.add(slider);
		slideRow.add(Box.createHorizontalStrut(3));
		slideRow.add(rightFrame);
		
		waitFieldRow.add(new JLabel("Check for "));
		waitFieldRow.add(waitField);
		waitFieldRow.add(new JLabel(" s."));
		waitFieldRow.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("LOS life"),
				BorderFactory.createEmptyBorder(3, 3, 3, 3)));
		
		temp.setBorder(BorderFactory.createTitledBorder("Event start"));
		
		temp.add(timeRow);
		temp.add(slideRow);
		this.add(temp);
		this.add(Box.createVerticalStrut(5));
		this.add(waitFieldRow);
	}
	
	public void modify() {
		try {
			event.setEndTime(event.getStartTime()+Double.parseDouble(waitField.getText()));
		}
		catch (NumberFormatException e) {}
	}

	private void setTime(int time) {
		min.setText(""+time/60);
		sec.setText(""+(time-60*(time/60)));
	}
	
	private double getTime() {
		try {
			return 60*Double.parseDouble(min.getText())+Double.parseDouble(sec.getText());
		}
		catch (NumberFormatException e) {return 0;}
	}
	
	public void focusGained(FocusEvent arg0) {
		waitField.selectAll();
	}

	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("leftFrame"))
			slider.setValue(slider.getValue()-10);
		if (e.getActionCommand().equals("rightFrame"))
			slider.setValue(slider.getValue()+10);
	}

	public void stateChanged(ChangeEvent e) {
		setTime(slider.getValue()/100);
		event.setStartTime(((double)slider.getValue())/100);
		csc.completeStaticUpdate();
	}

}
